home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 033a / cal14s23.zip / QREAD.ASM < prev    next >
Assembly Source File  |  1989-06-02  |  5KB  |  188 lines

  1.  
  2.         page ,132
  3. ;
  4. ; Copyright 1987, 1989 Samuel H. Smith;  All rights reserved
  5. ;
  6. ; This is a component of the ProDoor System.
  7. ; Do not distribute modified versions without my permission.
  8. ; Do not remove or alter this notice or any other copyright notice.
  9. ; If you use this in your own program you must distribute source code.
  10. ; Do not use any of this in a commercial product.
  11. ;
  12. ;
  13. ; Qread - quick version of ReadLn for text files
  14. ;         Used by Qread unit.
  15. ;
  16. ; Written by Samuel Smith, 11-19-88
  17. ;
  18.  
  19. code segment byte public
  20.         assume cs:code
  21.         public qReadLn
  22.  
  23. ; -------------------------------------------------------------
  24. ;
  25. ; structure of text file record
  26. ;
  27. textfile struc
  28.         handle  dw ?    ;dos handle
  29.         mode    dw ?    ;open mode
  30.         bufSize dw ?    ;size of file buffer
  31.         priv1   dw ?
  32.         bufPos  dw ?    ;position of next read within file buffer
  33.         bufEnd  dw ?    ;position past last byte of file buffer
  34.         bufPtr  dd ?    ;pointer to file file buffer
  35. textfile ends
  36.  
  37.         
  38. ; -------------------------------------------------------------
  39. ;
  40. ; procedure qReadLn( var fd: text;
  41. ;                    var dest: string;
  42. ;                    maxlen: word );
  43. ;
  44.  
  45. ; structure of stack frame --
  46. fileptr equ dword ptr [bp+0ch]  ;pointer to text file record
  47. destptr equ dword ptr [bp+08h]  ;pointer to destination string
  48. maxlen  equ word  ptr [bp+06h]  ;maximum length of destination string
  49. curpos  equ dx
  50. curend  equ bx
  51.  
  52. qReadLn proc far
  53.         push bp
  54.         mov bp,sp               ;create stack frame
  55.         push ds
  56.  
  57.         lds si,fileptr          ;ds:si -> textfile
  58.         mov curpos,bufpos[si]   ;current file buffer position
  59.         mov curend,bufend[si]   ;end of file buffer position
  60.  
  61.         lds si,bufptr[si]       ;ds:si -> file buffer
  62.         add si,curpos           ;            [bufpos]
  63.  
  64.         les di,destptr          ;es:di -> dest
  65.         inc di
  66.  
  67.         mov cx,maxlen           ;initial destination space
  68.         jmp short NextChar      ;get first character
  69.  
  70. ;
  71. ; main character loop
  72. ;
  73. StoreChar:
  74.         stosb                   ;dest[len++] = c
  75.  
  76. ; process next character in file buffer
  77. NextChar:
  78.         cmp curpos,curend       ;end of file buffer?
  79.         jz NextBuffer
  80.  
  81. ; have a character in the file buffer - get it
  82. HaveChar:
  83.         lodsb                   ;c = buf[bufptr++]
  84.         inc curpos
  85.  
  86.         cmp al,26
  87.         jle CheckControl
  88.  
  89. ; it is a normal character - add it to the destination buffer
  90. NormChar:
  91.         loop StoreChar          ;dec cx, jnz
  92.         jmp short qEndLine
  93.  
  94. ;
  95. ; check control characters
  96. ;
  97. CheckControl:
  98.         cmp al,26               ;^Z? end of file
  99.         jz qEndFile
  100.         cmp al,10               ;lf? end of line
  101.         jz qEndLine
  102.         cmp al,13               ;cr? skip it
  103.         jz NextChar
  104.         jmp short NormChar
  105.  
  106. ;
  107. ; file buffer is empty - get another one
  108. ;
  109. NextBuffer:
  110.         lds si,fileptr          ;ds:si -> textfile
  111.         call qFillBuf
  112.  
  113.         mov curend,bufend[si]
  114.         xor curpos,curpos       ;bufpos=0
  115.         lds si,bufptr[si]       ;ds:si -> file buffer
  116.  
  117.         cmp curend,curpos
  118.         jnz HaveChar
  119.  
  120. ;
  121. ; end of file - return dest = ^Z unless dest has data in it
  122. ;
  123. qEndFile:
  124.         cmp cx,maxlen
  125.         jnz qEndLine
  126.  
  127.         mov al,26
  128.         stosb                   ;dest = ^Z
  129.         dec cx
  130.  
  131. ;
  132. ; end of line - set line length and return
  133. ;
  134. qEndLine:
  135.         lds si,fileptr          ;ds:si -> textfile
  136.         mov bufpos[si],curpos   ;update file buffer position
  137.  
  138.         mov ax,maxlen
  139.         sub ax,cx               ;calculate destination bytes used
  140.         les di,destptr          ;es:di -> dest
  141.         mov es:[di],al          ;update destination length
  142.  
  143.         pop ds
  144.         pop bp
  145.         ret 10                  ;dispose of 10 parameter bytes
  146. qReadLn endp
  147.  
  148.  
  149. ; -------------------------------------------------------------
  150. ;
  151. ; fill file buffer
  152. ;
  153. ; from  handle[si]      dos handle
  154. ;       bufptr[si]      data buffer
  155. ;       bufsize[si]     data buffer size
  156. ;
  157. ; sets  bufpos[si]      to 0
  158. ;       bufend[si]      to bytes read
  159. ;
  160. ; preserves dx, cx, bx, ds, es, si, di
  161. ;
  162.  
  163. qFillBuf proc near
  164.         push dx
  165.         push cx
  166.         push bx
  167.  
  168.         mov ax,3f00h            ;read
  169.         mov bx,handle[si]       ;dos handle
  170.         mov cx,bufsize[si]      ;file buffer size
  171.         push ds
  172.         lds dx,bufptr[si]       ;file buffer pointer
  173.         int 21h                 ;perform the read
  174.         pop ds
  175.         mov bufend[si],ax       ;bytes read
  176.         mov bufpos[si],0        ;bytes used
  177.  
  178.         pop bx
  179.         pop cx
  180.         pop dx
  181.         ret
  182. qFillBuf endp
  183.  
  184. code ends
  185. end
  186.  
  187.  
  188.